package com.jn.algorithm.day002.question6;

/**
 * //Z字形变换
 *
 * @author 江南大学1033190417
 * @date 2022/4/16 20:23
 */
public class Solution {
    public String convert(String s, int numRows) {
        if (numRows == 1)
            return s;
        int lie = (s.length() / ((numRows - 1) * 2)) * (numRows - 1) + numRows;
        char[][] res = new char[numRows][lie];
        int count = 0;
        for (int i = 0; i < lie; i++) {
            if (i % (numRows - 1) != 0) {//如果是那个转折的点
                res[numRows - 1 - i % (numRows - 1)][i] = s.charAt(count++);
                if (count == s.length()) {
                    break;
                }
            } else {
                for (int j = 0; j < numRows; j++) {
                    res[j][i] = s.charAt(count++);
                    if (count == s.length())
                        break;
                }
            }
        }
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < lie; j++) {
                if (res[i][j] != 0)
                    result.append(res[i][j]);
            }
        }
        return result.toString();
    }


    public static void main(String[] args) {
        Solution solution = new Solution();
        String s = "ABCDE";
        System.out.println(solution.convert(s, 4));
    }
}
